import { Component, Prop } from 'vue-property-decorator';
import AbpBase from "@/lib/abpbase";

@Component
export default class CreateMixin extends AbpBase {
  @Prop({ type: Boolean, default: false }) value: boolean;

  saving = false;

  // TODO:
  async saveItem() {
    // const { schoolType, schoolCategory, runSchoolType } = this.school
    // this.school.schoolTypeName = (this.schoolTypes.find((t) => t.id === schoolType) || {})['name']
    // this.school.schoolCategoryName = (this.schoolCategories.find((t) => t.id === schoolCategory) || {})['name']
    // this.school.runSchoolTypeName = (this.runTypes.find((t) => t.id === runSchoolType) || {})['name']
    // await this.$store.dispatch({
    //   type: 'school/create',
    //   data: this.school
    // });
    return new Promise<any>(() => false);
  }
  save() {
    const createForm = this.$refs.createForm;
    
    if (!!createForm) {
      (createForm as any).validate(async (valid: boolean) => {
        if (valid) {
          try {
            this.saving = true;
            const resetFlag = await this.saveItem()
            this.$Message.success('保存成功')
            if (!!createForm && !!resetFlag) {
              (createForm as any).resetFields();
            }
            this.$emit('save-success');
            this.$emit('input', false);
          } catch(err) {
            this.$Message.error('保存失败')
          } finally {
            this.saving = false;
          }
        }
      })
    } else {
    }
  }
  cancel() {
    this.$emit('input', false);
  }

  // TODO
  async onHide() {
    // this.$store.commit('school/edit', null);
    // this.school = new CreateOrUpdateSchoolInfo();
  }
  // TODO
  async onShow() {
    // this.getCities()
    // await this.getSchoolTypes()
    // await this.getSchoolCategories()
    // await this.getRunTypes()
    // if (!!this.$store.state.school.editSchool) {
    //   ajax.get('/api/services/app/SchoolInformation/GetSchoolInformationForEdit', {
    //     params: {
    //       Id: this.$store.state.school.editSchool.id,
    //     }
    //   }).then((res) => {
    //     if (res.data.success) {
    //       Object.assign(this.school, res.data.result.schoolInforOutput)
    //     }
    //   });
    // }
  }
  async visibleChange(value: boolean) {
    if (!value) {
      await this.onHide();
      const createForm = this.$refs.createForm as any
      if (!!createForm) {
        createForm.resetFields();
      }
    } else {
      await this.onShow();
    }
    this.$emit('input', value);
  }

  // TODO:
  formRule: any;

  // TODO
  async createdInit() {
  }

  async created() {
    await this.createdInit()
  }
  
  // async formLoaded() {
  //   const createForm = this.$refs.createForm;
  //   if (!!createForm) {
  //     (createForm as any).resetFields();
  //   }
  //   await this.createdInit()
  // }
}